﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Windows.Input;
using System.Collections;
using Passwords.Utils;

namespace Passwords.Models
{
    [DataContract(Namespace = "http://schemas.datacontract.org/2004/07/PasswordsService")]
    public class FieldContract : Entity
    {
        [DataMember(Name="Name")]
        private string name;

        [DataMember(Name = "Value")]
        private string value;

        [DataMember(Name = "FieldType")]
        private int fieldType;

        private bool isPassword;

        [IgnoreDataMember]
        public double Strength
        {
            get
            {
                return (double)PasswordUtility.GetStrength(value);
            }
        }

        [IgnoreDataMember]
        public bool IsPassword
        {
            get { return isPassword; }
            private set
            {
                if (isPassword != value)
                {
                    isPassword = value;
                    OnPropertyChanged("IsPassword");
                }
            }
        }


        [IgnoreDataMember]
        public PhoneInputScopeNameValue InputScope
        {
            get { return (PhoneInputScopeNameValue)FieldType; }
            set
            {
                FieldType = (int)value;
            }
        }

        [IgnoreDataMember]
        public bool IsCustom { get; set; }

        [DataMember]
        public string Id { get; set; }

        public string Name
        {
            get { return name; }
            set
            {
                if (name != value) name = value;
                OnPropertyChanged("Name");
            }
        }


        //        [DataMember(IsRequired = false, EmitDefaultValue = false)]
        public string Value
        {
            get { return this.value; }
            set
            {
                if (this.value != value) this.value = value;
                OnPropertyChanged("Value");
                if (isPassword) OnPropertyChanged("Strength");
            }
        }

        public int FieldType
        {
            get { return fieldType; }
            set
            {
                if (fieldType != value)
                {
                    fieldType = value;
                    OnPropertyChanged("FieldType");
                    OnPropertyChanged("InputScope");
                    IsPassword = value == (int)PhoneInputScopeNameValue.Password;
                }
            }
        }

        [DataMember(IsRequired = false, EmitDefaultValue = false)]
        public int MinChars { get; set; }

        [DataMember(IsRequired = false, EmitDefaultValue = false)]
        public int MaxChars { get; set; }



        public override string ToString()
        {
            return string.Format("{0}={1}   (id={2})", Name, Value, Id);
        }
    }
}
